import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Row, message } from 'antd';
import { ModalC, UploadC, FormGroup, DATA_ENTRY } from '../../components';
import applicationModuleData from './config';
import moment from 'moment';

const formItemLayout = {
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 16,
  },
};
class uploadModalC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      applicationModule: '', // 应用模块
      lackDate: true, // 日期是否选择季度
      fileId: '', // 文件id
      fileName: '', // 文件名
      uploadCref: undefined,
    };
  }

  FormGroupRefList = React.createRef();

  componentDidMount() {
    if (this.props.isDetails || this.props.isOnlyTest) {
      this.setState({ applicationModule: this.props.isOnlyTest ? '1' : this.props.modalData.applicationModule });
    }
  }

  // 响应上传确定
  handleOk = () => {
    if (this.props.isDetails) {
      // 详情点击确定直接关闭
      this.props.uploadCancel();
      return;
    }
    this.FormGroupRefList.current?.getFormGroupData().then((fieldsValue) => {
      const fileList = this.state.uploadCref?.getFileList();
      const newfileList = fileList?.filter((item) => item.value != undefined);
      if ((Array.isArray(newfileList) && newfileList.length == 0) || newfileList == undefined) {
        message.error('请重新上传附件');
        return;
      }
      const uploadData = {};
      let businessField = {};
      // 后端要求配置的字段，通过businessField字段json格式传参

      switch (this.state.applicationModule) {
        // 应用模块为绩效分析报告
        case '0':
          businessField.productNum = fieldsValue.productNum.toString();
          businessField.year = moment(fieldsValue.year).format('YYYY');
          businessField.quarter = fieldsValue.quarter;
          break;
        case '1':
          businessField.productNum = fieldsValue.productNum.toString();
          businessField.reportDate = moment(fieldsValue.reportDate).format('YYYY-MM-DD');
          businessField.needPenetrate = fieldsValue.needPenetrate;
          break;
        default:
          businessField = {};
      }
      uploadData.tenant = fieldsValue.tenant;
      uploadData.applicationModule = fieldsValue.applicationModule;
      uploadData.fileList = newfileList;
      uploadData.businessField = JSON.stringify(businessField);
      this.props.dispatch({
        type: 'fileUploadM/uploadFile',
        payload: {
          ...uploadData,
        },
        callback: (data) => {
          if (data) {
            this.props.uploadOk();
          }
        },
      });
    });
  };

  changeModule = (e) => {
    this.setState({ applicationModule: e });
    // 状态切换之后 清空文件数据
    this.state.uploadCref?.setState({
      fileList: [],
    });
    this.FormGroupRefList.current?.formRef.current?.resetFields(['productNum']);
  };

  render() {
    const { isOnlyTest, isDetails, modalData, isVisible, loading } = this.props;
    const modalProps = {
      destroyOnClose: true,
      visible: isVisible,
      title: '执行上传',
      width: 800,
      confirmLoading: loading.effects['fileUploadM/uploadFile'] === true,
      onCancel: () => {
        this.props.uploadCancel();
      },
      onOk: () => {
        this.handleOk();
      },
    };
    const modalInitialValue = isDetails ? JSON.parse(modalData.businessField) : {};
    return (
      <ModalC {...modalProps}>
        <FormGroup
          onRef={(ref) => {
            this.FormGroupRefList.current = ref;
          }}
          autoLayout={false}
          rowNumber={2}
          dataSource={[
            {
              dataIndex: 'tenant',
              type: DATA_ENTRY.TENANT,
              formItemLayout,
              span: 12,
              initialValue: isDetails ? modalData.tenant : null,
              rules: [{ required: true, message: '请选择租户' }],
              disabled: isDetails,
              positionBody: true,
            },
            {
              dataIndex: 'applicationModule',
              type: DATA_ENTRY.SELECT,
              formItemLayout,
              title: '应用模块',
              span: 12,
              initialValue: isDetails ? modalData.applicationModule : isOnlyTest ? '1' : null,
              rules: [{ required: true, message: '请选择模块' }],
              disabled: isOnlyTest ? true : isDetails,
              positionBody: true,
              allowClear: true,
              dataSource: applicationModuleData,
              onChange: this.changeModule,
            },
            {
              hide: !(this.state.applicationModule === '0'),
              title: '报告日期',
              span: 12,
              type: DATA_ENTRY.SIMPLEQUARTERC,
              formItemLayout,
              yearKey: 'year',
              quarterKey: 'quarter',
              initialValueYear: modalInitialValue?.year,
              initialValueQuarter: modalInitialValue?.quarter,
              highCurrentDate: true,
              rules: [[{ required: true, message: '请选择年份' }], [{ required: true, message: '请选择季度' }]],
              disabled: isDetails,
            },
            {
              hide: !(this.state.applicationModule === '0'),
              dataIndex: 'productNum',
              type: DATA_ENTRY.INPUTNUMBER,
              title: '产品数量',
              span: 12,
              formItemLayout,
              initialValue: modalInitialValue?.productNum,
              rules: [{ required: true, message: '请填写产品数量' }],
              disabled: isDetails,
              splitStyle: true,
              precision: 0,
              min: 1,
            },
            {
              span: 12,
              hide: !(this.state.applicationModule === '1'),
              dataIndex: 'reportDate',
              title: '报告日期',
              type: DATA_ENTRY.SINGLEDATE,
              formItemLayout,
              initialValue: modalInitialValue?.reportDate ? moment(modalInitialValue?.reportDate) : undefined,
              rules: [{ required: true, message: '请选择日期' }],
              disabled: isDetails,
            },
            {
              hide: !(this.state.applicationModule === '1'),
              dataIndex: 'productNum',
              type: DATA_ENTRY.INPUTNUMBER,
              title: '产品数量',
              span: 12,
              formItemLayout,
              initialValue: modalInitialValue?.productNum,
              disabled: isDetails,
              splitStyle: true,
              precision: 0,
              min: 1,
            },
            {
              hide: !(this.state.applicationModule === '1'),
              type: DATA_ENTRY.SELECT,
              dataIndex: 'needPenetrate',
              formItemLayout,
              title: '是否穿透',
              isTrueFalseJudge: true,
              initialValue: modalInitialValue?.needPenetrate || '0',
              allowClear: true,
              disabled: isDetails,
            },
            {
              dataIndex: 'upload',
              title: '附件上传',
              type: DATA_ENTRY.FORMOTHERC,
              rules: [{ required: true, message: '请上传附件' }],
              span: 24,
              Children: (
                <Row type='flex' align='left'>
                  <UploadC
                    type={1}
                    showUploadList={true}
                    title='附件选择'
                    value={isDetails && modalData?.fileList}
                    onRef={(e) => this.setState({ uploadCref: e })}
                    disabled={this.state.applicationModule == '' ? true : isDetails}
                    isDelete={!isDetails}
                    multiple={this.state.applicationModule != '1'}
                    primaryProps={{ helper: '上传文件不能大于200M' }}
                    max={this.state.applicationModule == '1' ? 1 : undefined}
                  />
                </Row>
              ),
              formItemLayout: {
                labelCol: {
                  span: 4,
                },
                wrapperCol: {
                  span: 20,
                },
              },
            },
          ]}
        />
      </ModalC>
    );
  }
}
export default connect(({ fileUploadM, loading }) => ({ fileUploadM, loading }))(uploadModalC);
